package org.ltc.alliance.service.clubuser;

import org.ltc.alliance.constant.ErrorConst;
import org.ltc.alliance.constant.StatusConst;
import org.ltc.alliance.dao.ClubUserDao;
import org.ltc.alliance.exception.BusinessException;
import org.ltc.alliance.model.ClubUserDomain;
import org.ltc.alliance.service.club.ClubService;
import org.ltc.alliance.service.power.PowerService;
import org.ltc.alliance.service.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class ClubUserServiceImpl implements ClubUserService{

    @Autowired
    private ClubUserDao clubUserDao;
    @Autowired
    private ClubService clubService;
    @Autowired
    private UserService userService;

    @Override
    public void saveApplyInfo(ClubUserDomain clubUser) {

        // 检查社团是否存在且可以正常招收成员
        clubService.clubStatusNormalByCid(clubUser.getCid());
        // 检查用户是否存在
        userService.userIdExist(clubUser.getUid());

        // 检查社团与用户之间是否已存在关系
        ClubUserDomain clubUserExist = clubUserDao.getInfoByCidAndUid(clubUser.getCid(),clubUser.getUid());
        if (null != clubUserExist){
            if (clubUserExist.getCustatus().equals(StatusConst.ClubUser.APPLYING))
//                throw new BusinessException(ErrorConst.ClubUser.APPLY_MULTIPLE);
                return;
            clubUserDao.alterStatusByCidAndUid(clubUser.getCid(),clubUser.getUid(), StatusConst.ClubUser.APPLYING);
        }
        else {
            clubUser.setCustatus(StatusConst.ClubUser.APPLYING);
            clubUserDao.saveClubUserInfo(clubUser);
        }

    }

    @Override
    public List<ClubUserDomain> getListByCidAndStatus(Integer cid, StatusConst.ClubUser status) {

        // 检查社团是否存在且可以正常招收成员
        clubService.clubStatusNormalByCid(cid);

        // 检查是否接收到社团状态
        if(Objects.isNull(status)) {
            throw new BusinessException(ErrorConst.ClubUser.STATUS_NOT_EXIST);
        }

        return clubUserDao.getListByCidAndStatus(cid, status);

    }

    @Override
    public List<ClubUserDomain> getListByUidAndStatus(Integer uid, StatusConst.ClubUser status) {
        userService.userStatusNormalByUid(uid);
        // 检查是否接收到社团状态
        if(Objects.isNull(status)) {
            throw new BusinessException(ErrorConst.ClubUser.STATUS_NOT_EXIST);
        }

        return clubUserDao.getListByUidAndStatus(uid, status);
    }

    @Override
    public void alterStatusByCidAndUid(Integer cid, Integer uid, Boolean pass,
                                       StatusConst.ClubUser preStatus) {
        // 检查用户、社团是否存在
        userService.userIdExist(uid);
        clubService.clubIdExist(cid);

        if (Objects.isNull(pass)) {
            throw new BusinessException(ErrorConst.ClubUser.STATUS_RESULT_LOST);
        }

        // 检查修改对象是否符合要求
        StatusConst.ClubUser clubUserStatus = clubUserDao.getStatusByCidAndUid(cid, uid);
        if (!clubUserStatus.equals(preStatus)) {
            throw new BusinessException(ErrorConst.ClubUser.STATUS_ALTER_MISMATCH);
        }

        if (preStatus.equals(StatusConst.ClubUser.APPLYING)) {
            clubUserDao.alterStatusByCidAndUid(cid, uid,
                    pass ? StatusConst.ClubUser.NORMAL : StatusConst.ClubUser.REJECTED);
        } else {
        throw new BusinessException(ErrorConst.ClubUser.STATUS_ALTER_MISMATCH);
        }
    }

    @Override
    public void quitClub(Integer cid, Integer uid) {

        // 检查社团与用户之间是否已存在关系
        this.userInClub(cid, uid);

        clubUserDao.alterStatusByCidAndUid(cid,uid, StatusConst.ClubUser.REVOKED);

    }

    @Override
    public void kickUser(Integer provider, Integer receiver, Integer cid) {
        this.quitClub(receiver, cid);
    }

    @Override
    public void userInClub(Integer cid, Integer uid) {
        ClubUserDomain clubUser = clubUserDao.getInfoByCidAndUid(cid, uid);
        if (!clubUser.getCustatus().equals(StatusConst.ClubUser.NORMAL)) {
            throw BusinessException.withErrorCode(ErrorConst.ClubUser.USER_NOT_IN_CLUB);
        }
    }

    @Override
    public Boolean userIsInClub(Integer cid, Integer uid) {
        ClubUserDomain clubUser = clubUserDao.getInfoByCidAndUid(cid, uid);
        if (null == clubUser) return Boolean.FALSE;
        else {
            return (clubUser.getCustatus().equals(StatusConst.ClubUser.NORMAL)
                    ? Boolean.TRUE : Boolean.FALSE);
        }
    }
}
